package com.cy.dda.service.org.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.dda.dao.mapper.org.OrgMapper;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.domain.org.Org;
import com.cy.dda.model.query.org.CustomerOrgListQuery;
import com.cy.dda.model.query.org.OrgListAdminQuery;
import com.cy.dda.model.vo.org.CustomerOrgListVO;
import com.cy.dda.model.vo.org.OrgListAdminVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.org.OrgService;

/**
 * 委案机构 OrgService
 *
 * @author: tanyilin
 * @create: 2018年11月7日 下午2:12:39
 */
@Service
public class OrgServiceImpl extends BaseService implements OrgService {

	@Autowired
	private OrgMapper orgMapper;

	@Override
	@Transactional
	public void addOrg(Org org) {
		
		Org dbOrg = new Org();
		dbOrg.setName(org.getName());
		dbOrg = orgMapper.selectOne(dbOrg);
		state(dbOrg == null, "该机构名称已存在");
		
		Date time = new Date();
		org.setCreateTime(time);
		org.setUpdateTime(time);
		orgMapper.insertSelective(org);
	}

	@Override
	public PageBean<OrgListAdminVO> selectOrgListAdmin(OrgListAdminQuery query) {
		int total = orgMapper.selectOrgListAdminCount(query);
		List<OrgListAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = orgMapper.selectOrgListAdmin(query);
		}
		
		return new PageBean<OrgListAdminVO>(list, total);
	}

	@Override
	public PageBean<CustomerOrgListVO> selectCustomerOrgList(CustomerOrgListQuery query) {
		int total = orgMapper.selectCustomerOrgListCount(query);
		List<CustomerOrgListVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = orgMapper.selectCustomerOrgList(query);
		}
		return new PageBean<CustomerOrgListVO>(list, total);
	}

	@Override
	public List<Org> selectAllOrgList() {
		List<Org> list = orgMapper.selectAll();
		list.stream().forEach(o -> {
			o.setRemark(null);
			o.setCreateTime(null);
			o.setUpdateTime(null);
		});
		return list;
	}
	
	
	
	
}
